home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7714 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 27 Feb 1996 15:32:34 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb27083234@qcd.lanl.gov>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu>
  9.     <4gtab6$acb@ceylon.gte.com>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: Brenda's message of 26 Feb 1996 21:56:54 GMT
  14.  
  15. In article <4gtab6$acb@ceylon.gte.com>
  16. Brenda <g051286> writes:
  17. <snip>
  18. B: >|>     char myarray[10];
  19. <snip>
  20. B: >Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
  21. B: >a pointer to 'myarray'.
  22. B: 
  23. B: Um, that's not correct.  myarray is DEFINITELY a pointer!  As
  24. declared above, 
  25. B: it is a constant pointer to 10 contiguous char datatypes.  myarray is an
  26.  
  27. Don't fill this newsgroup with garbage, we don't need this discussion
  28. once again. Please chastise your service provider in the strongest
  29. possible terms: it is common decency on their part not to flood proper
  30. discussions with newbies who have no idea of nettiquette.
  31.  
  32. Most newsgroups have FAQs (Frequently Asked Questions with their
  33. answers), and most are available by anonymous ftp from
  34. rtfm.mit.edu. It is considered proper to retrieve them and read them
  35. before posting, so as not to waste everybody's time with very common
  36. misunderstandings. If you read and understand what the FAQ is saying,
  37. and you think it is wrong, and you have enough reason to believe so,
  38. that point is certainly welcome. Bringing it up without reading the
  39. FAQ gets no one's sympathy.
  40.  
  41. In any case, it is polite to listen to a conversation a while before
  42. jumping in. The same is true of USENET: and I am sure if you had
  43. listened patiently for a week or two, you would have seen why you are
  44. mistaken. 
  45.  
  46. In any case, an array in C is not a pointer, just as `int i' does not
  47. declare a pointer. `myarray' above and `i' in my declaration both
  48. declare a region of storage, one for a sequence of 10 chars, and the
  49. other for one int. `int *ptr', on the other hand, reserves storage for
  50. a pointer to int. Note that `myarray' is not associated with any
  51. storage for a pointer, and `ptr' is not associated with any storage
  52. for an int. This can be easily verified by applying the `sizeof'
  53. operator to each of them. One can easily verify as well that `char
  54. myarray[10]' followed by `extern char *myarray' is a recipe for
  55. disaster. 
  56.  
  57. In most occurances of an object in an expression (for example when one
  58. is not taking the address (&) or the sizeof the object), C evaluation
  59. calls for the value of an object. The value of most objects is the
  60. value stored in them, but the value of an array object is actually
  61. independent of the value stored in the memory location that constitute
  62. the array: it is instead a pointer to the _first_ of the sequence of
  63. objects that constitute it. Thus it is completely incorrect to call it
  64. a `constant pointer to 10 contiguous char datatypes'. What is true,
  65. however, is that _unless_ it is the operand of & or sizeof (the actual
  66. situation under discussion), the value of myarray is a `pointer to the
  67. first of 10 contiguous char objects'. Note that it is very difficult
  68. to talk of a value being a `constant', after i=5, is &i a constant
  69. pointer to an int? Is i a constant integer 5?
  70.  
  71. B: ADDRESS whereas *(myarray + 5) or myarray[5] is the 6th element in
  72. the array. 
  73.  
  74. Yes. In *(myarray+5) or myarray[5] it is the value of myarray that
  75. contributes. This indeed is a pointer.
  76.  
  77. B: The difference between an array and something like "char
  78. *p=myarray", is that 
  79. B: you can say p++, but you can't say myarray++.  You shouldn't say &myarray
  80.  
  81. The ++ operator needs a `modifiable lvalue' which is a technical term.
  82. What it means is that the expression must designate an object
  83. (e.g. (i+j) will not do: that expression designates a value and not an
  84. object), the object designator must not have a `const' qualified type,
  85. or if it is a struct, union or array, none of its elements or fields
  86. may have a const qualified type, (i.e. after `const int i', i++ is
  87. disallowed), and it shouldn't be an array.
  88.  
  89. Actually, even if the C standard did not put in the special rule for
  90. the array, you can easily see it wouldn't have made sense anyway. If
  91. you wrote myarray = myarray + 1, by the above rules, myarray+1 would be
  92. a pointer which you would have to assign to an array (if the array
  93. hadn't yet decayed to a pointer) or to a value (if it had). Neither of
  94. them makes the slightest sense, so, it is best not to even think of
  95. these things.
  96.  
  97. B: either because myarray is a constant, but I read that on some compilers
  98. B: scanf ignores the dereferencing and does not bother to warn you.
  99.  
  100. This is where you are most wrong. &myarray is very useful in some
  101. contexts. It is a pointer to the entire array, and such pointers have
  102. their uses. You are right in pointing out that passing them to scanf
  103. invokes undefined behaviour. 
  104.  
  105. B: Hope this helps.
  106.  
  107. Incorrect statements rarely help. Please read the FAQ, it is an
  108. excellent source of information about C, and the product of a
  109. considerable effort on the part of Steve Summit (and others).
  110.  
  111. Cheers
  112. Tanmoy
  113. --
  114. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  115. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  116. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  117. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  118. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  119. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  120.